/**
 * 能力评估表详情页
 */
import React from 'react';
import { history } from 'umi'
import { Form, Card, Button, Divider, Popconfirm, message } from 'antd';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import pageTabUtil from '@/utils/pageTabUtil';
import authHelper from '@/utils/auth/helper';
// import { BottomAction } from '@/components';
// import { fetchDutyList } from '@/services/components/userform';
import { useRequest, defaultFormatResult } from '@/hooks';
// import { InfoIcon, FormHeader } from '@/molecules';
import { FormHeader } from '@/molecules';
import { fetchOrg } from '@/containers/service';
import { getAbilityEvaluationDetail, delAbilityEvaluation } from './services';
import { AbilityDetail } from '../containers';
import styles from './index.less';

const { checkCode } = authHelper;

const InformalRuleDetail = ({ location }) => {
    const { query } = location;

    // 组织机构数据
    const { data: orgTree, loading: orgLoading } = useRequest(
        () =>
            fetchOrg({
                organizationPermissions: 0,
                showTreeFlag: 0
            }),
        { cacheKey: 'orgTree', ...defaultFormatResult }
    );

    // 获取能力评估表详情
    const { data, loading } = useRequest(() => getAbilityEvaluationDetail(query?.id), {
        refreshDeps: [query?.id],
        ready: !!query?.id,
        ...defaultFormatResult
    });

    // 删除设置
    const { loading: delLoading, run: delItem } = useRequest(delAbilityEvaluation, {
        manual: true,
        onSuccess: res => {
            const { code, msg } = res;
            if (code === 10000) {
                message.success('操作成功');
                pageTabUtil.goBack();
                return;
            }
            message.error(msg);
        }
    });

    const renderOrganizationNames = text => {
        if (data?.common === 1) {
            return '不限';
        }
        if (text && text.length > 0) {
            const orgList = text.map(item => orgTree?.find(it => it.organizationCode === item));
            const names = orgList.map(item => item?.organizationName);
            if (names.filter(it => it).length === 0) {
                return '';
            }
            const tempArr = [];
            for (let i = 0; i < orgList.length; i += 1) {
                const element = orgList[i];
                if (tempArr.includes(i)) {
                    // eslint-disable-next-line no-continue
                    continue;
                }
                for (let j = i + 1; j < orgList.length; j += 1) {
                    const element2 = orgList[j];
                    if (element2.organizationName === element.organizationName) {
                        tempArr.push(j);
                        const parentOrgName1 = orgTree?.find(
                            it => it.organizationPath === element.parentOrganizationPath
                        )?.organizationName;
                        if (parentOrgName1) {
                            names[i] = `${parentOrgName1}-${element.organizationName}`;
                        }

                        const parentOrgName2 = orgTree?.find(
                            it => it.organizationPath === element2.parentOrganizationPath
                        )?.organizationName;
                        if (parentOrgName2) {
                            names[j] = `${parentOrgName2}-${element2.organizationName}`;
                        }
                    }
                }
            }
            return names.join('、');
        }
        return '--';
    };

    const showBottomAction =
        data &&
        (checkCode('office:turnover:setting:informal:add') ||
            checkCode('office:turnover:setting:informal:edit') ||
            checkCode('office:turnover:setting:informal:delete'));
    return (
        <PageHeaderWrapper>
            <Card
                title="能力评估表详情"
                bordered={false}
                loading={loading || orgLoading}
                extra={
                    showBottomAction && (
                        <div className={styles.cardExtra}>
                            {checkCode('office:turnover:setting:informal:delete') && data?.common !== 1 && (
                                <Popconfirm
                                    title="确定删除该评估表吗?"
                                    onConfirm={() => delItem(query?.id)}
                                    okText="确定"
                                    cancelText="取消"
                                    placement="bottom"
                                    getPopupContainer={trigger => trigger.parentElement}
                                >
                                    <Button loading={delLoading}>删除</Button>
                                </Popconfirm>
                            )}
                            {checkCode('office:turnover:setting:informal:add') && (
                                <Button
                                    type="primary"
                                    ghost
                                    disabled={delLoading}
                                    onClick={() => {
                                        history.push({
                                            pathname:
                                                '/office/personnelManagement/onboardAndLeave/setting/informal/evaluation/add',
                                            query: { id: query?.id }
                                        });
                                    }}
                                >
                                    复制
                                </Button>
                            )}
                            {checkCode('office:turnover:setting:informal:edit') && (
                                <Button
                                    type="primary"
                                    disabled={delLoading}
                                    onClick={() => {
                                        history.push({
                                            pathname:
                                                '/office/personnelManagement/onboardAndLeave/setting/informal/evaluation/edit',
                                            query: { id: query?.id }
                                        });
                                    }}
                                >
                                    编辑
                                </Button>
                            )}
                        </div>
                    )
                }
            >
                {/* <FormHeader title="基本信息" /> */}
                <Form className={styles.formWrap2} style={{ maxWidth: 800 }}>
                    <Form.Item label="名称">{data?.projectName || '--'}</Form.Item>
                    <Form.Item label="适用组织机构">{renderOrganizationNames(data?.organizationCodeList)}</Form.Item>
                    <Form.Item label="每项最高分">5分</Form.Item>
                    <Form.Item label="说明">{data?.instructions || '--'}</Form.Item>
                </Form>
                <Divider />
                <FormHeader title="评估细项信息" />
                <div style={{ padding: '0 0 24px 0', maxWidth: 1080 }}>
                    <AbilityDetail data={data} className={styles.formWrap4} />
                </div>
            </Card>
        </PageHeaderWrapper>
    );
};

export default InformalRuleDetail;
